import wxParse from '../../wxParse/wxParse.js';
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: [
      {id:3,title:'医院动态'},
      {id:4,title:'行业动态'},
      {id:5,title:'培训视频'},
      {id:6,title:'科普知识'}
    ],
    category_id:3,
    detail:{},
    currentTab: 0,
    current_page: 1,
    last_page: 1,
    articleList:[]
  },
  navbarTap: function (e) {
    let app = this
    let navbar = app.data.navbar
    let index = e.currentTarget.dataset.idx
    app.setData({
      currentTab: index,
      category_id: navbar[index].id,
      current_page: 1,
      last_page: 1,
      articleList: []
    })
    app.getArticle()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  // 拨打电话
  iphoneNumber(e){
    const phoneNumber = e.currentTarget.dataset.type;
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
    })
  },

  openLocation(){
    let app = this
    wx.openLocation({
      latitude: parseFloat(app.data.detail.latitude),
      longitude: parseFloat(app.data.detail.longitude),
      success: function() {
        console.log('success');
      },
      fail: function(err) {
        console.log(err)
      }
    });
  },

  getDetail(){
    let app = this
    App._get('wei_chang_jing/getShopDetail', {}, function (result) {
      let detail = result
      let content = result.summary
      // 富文本转码
      if (content.length > 0) {
        wxParse.wxParse('content', 'html', content, app, 0);
      }
      app.setData({detail})
    });
  },

  /**
   * 跳转文章详情页
   */
  onTargetDetail(e) {
    wx.navigateTo({
      url: '../article/detail/index?article_id=' + e.currentTarget.dataset.id
    });
  },

  getArticle(){
    let app = this
    let articleList = app.data.articleList
    let current_page = app.data.current_page
    if (current_page > app.data.last_page) {
      return false
    }
    App._get('article/lists', {
      category_id:app.data.category_id,
      page:current_page
    }, function (res) {
      let array = res.data.list.data
      array.forEach(element => {
        articleList.push(element)
      })
      app.setData({
        articleList: articleList,
        current_page: current_page,
        last_page: res.data.list.last_page
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      category_id:3,
      current_page:1,
      last_page:1,
      articleList:[]
    })
    this.getDetail()
    this.getArticle()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 页面上拉触底事件的处理函数
    let app = this
    // 获取当前页面的页数并赋值给当前新的参数
    let current_page = app.data.current_page
    current_page++
    app.setData({
      current_page
    })
    app.getArticle()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})